package com.huiquan.section.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.section.service.SectionService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.constant.BaseContants;
import com.huiquan.framework.utils.RequestParamUtil;

@Controller
public class SectionController {

    @Autowired
    private SectionService sectionService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/section/section_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		int sexAgeFlag = BaseContants.FLAG_NO;

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			
			if(tabid.endsWith("_2")) {
				sexAgeFlag = BaseContants.FLAG_YES;
			}
			pageParam.put("sexAgeFlag", sexAgeFlag + "");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
			sexAgeFlag = Integer.parseInt(RequestParamUtil.getParamByRequestOrSession(req, pageParam, "sexAgeFlag"));
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String nameKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String sexKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "sexKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return sectionService.list(start, nameKey, exactSearchFlag, sexKey, sexAgeFlag,countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/section/set_sex.do")
	public ReturnData setSex(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String sex = req.getParameter("sex");
		return sectionService.setSex(id, sex, user);
	}

	@ResponseBody
	@RequestMapping(value = "/section/set_age.do")
	public ReturnData setAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String age = req.getParameter("age");
		Boolean flag = Boolean.parseBoolean(req.getParameter("flag"));
		return sectionService.setAge(id, age, flag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/section/submit_sex_age.do")
	public ReturnData submitSexAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String ids = req.getParameter("ids");
		return sectionService.submitSexAge(ids, user);
	}

}
